<template>
  <div class="p20 bg-white console h100">
    <div class="mt10 mb20">
      <el-button
        class="theme-btn"
        @click="handleAdd"
        v-if="permitIds.includes('merchant_conve_add')"
        >新增服务</el-button
      >
      <Search :placeholder="placeholder" @update="handleUpdate"></Search>
    </div>
    <el-table
      v-loading="loading"
      border
      stripe
      element-loading-text="拼命加载中"
      :data="tableData"
    >
      <el-table-column label="序号" type="index"></el-table-column>
      <el-table-column prop="name" label="服务名称"></el-table-column>
      <el-table-column prop="createDate" label="创建时间"></el-table-column>
      <el-table-column prop="operation" label="操作" width="280">
        <template slot-scope="scope">
          <el-button
            size="mini"
            class="theme-btn"
            @click="handleDetail(scope.row.id)"
            v-if="permitIds.includes('merchant_conve_load')"
            >查看详情</el-button
          >
          <el-button
            size="mini"
            @click="handleEdit(scope.row.id)"
            v-if="permitIds.includes('merchant_conve_update')"
            >修改</el-button
          >
          <el-button
            size="mini"
            @click="handleDelete(scope.row.id)"
            v-if="permitIds.includes('merchant_conve_delete')"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :pageTotal="pageTotal"
      @paginationChange="handleUpdate"
    ></Pagination>
    <ConvenientInfoDialog
      :title="title"
      :loading="loading"
      @update="handleUpdate"
      @close="handleClose"
      :convenientInfoId="convenientInfoId"
      :addConvenientInfoDialog="addConvenientInfoDialog"
      :editConvenientInfoDialog="editConvenientInfoDialog"
    ></ConvenientInfoDialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import Search from "@/components/search";
import Pagination from "@/components/pagination";
import ConvenientInfoDialog from "./components/convenient_info_dialog";
import { getAllConvenientInfo, deleteConvenientInfo } from "@/apis/merchant";

export default {
  name: "Convenient_info",
  data() {
    return {
      title: "",
      pageTotal: 0,
      tableData: [],
      convenientInfoId: 0,
      placeholder: "输入关键字搜索",
      addConvenientInfoDialog: false,
      editConvenientInfoDialog: false
    };
  },
  created() {
    this.$store.dispatch("load/setLoading", true);
    this.$store.dispatch("pagination/resetPageNum");
    this.getConvenientInfos(this.pageSize, this.pageNum);
  },
  computed: {
    ...mapGetters(["pageSize", "pageNum", "queryStr", "permitIds", "loading"])
  },
  methods: {
    async getConvenientInfos(pageSize, pageNum) {
      const { object } = await getAllConvenientInfo({
        pageNum,
        pageSize,
        queryStr: this.queryStr
      });

      this.pageTotal = object.total;
      this.tableData = object.records;
      this.$store.dispatch("load/setLoading", false);
    },
    handleUpdate() {
      this.getConvenientInfos(this.pageSize, this.pageNum);
    },
    handleDetail(id) {
      sessionStorage.setItem("convenientInfoId", id);
      this.$router.push({ name: "convenientBusiness" });
    },
    handleAdd() {
      this.title = "新增服务";
      this.addConvenientInfoDialog = !this.addConvenientInfoDialog;
    },
    handleClose() {
      this.addConvenientInfoDialog = false;
      this.editConvenientInfoDialog = false;
      this.$store.dispatch("load/setLoading", false);
    },
    handleDelete(id) {
      this.$confirm("此操作将删除该条, 是否继续?", "提示", {
        type: "warning",
        cancelButtonText: "取消",
        confirmButtonText: "确定"
      })
        .then(() => {
          this.$store.dispatch("load/setLoading", true);
          // deleteConvenientInfo({ convenientInfoId: id }).then(res => {
          //   if (res && res.code === "200") {
          //     this.$notify({
          //       title: res.msg,
          //       type: "success"
          //     });
          //     this.$store.dispatch("load/setLoading", false);
          //     this.handleUpdate();
          //   }
          // });
          this.$notify({
            title: "操作成功！",
            type: "success"
          });
          this.$store.dispatch("load/setLoading", false);
          this.handleUpdate();
        })
        .catch(() => {
          this.$message("已取消删除");
        });
    },
    handleEdit(id) {
      this.title = "编辑服务";
      this.convenientInfoId = id;
      this.editConvenientInfoDialog = !this.editConvenientInfoDialog;
    }
  },
  components: {
    Search,
    Pagination,
    ConvenientInfoDialog
  }
};
</script>

<style scoped lang="less"></style>
